package com.seteam.collabcrossword.windows;

import com.seteam.collabcrossword.CrosswordSolution;
import com.seteam.collabcrossword.Tile;
import net.miginfocom.swing.MigLayout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Board extends JPanel {
    private final Logger logger = LoggerFactory.getLogger(Board.class);
    private JTextField[][] tiles;
    private JPanel boardContainer;
    private MigLayout migLayout;
    private CrosswordSolution crossword;

    public Board(final CrosswordSolution crossword) {
        super();

        migLayout = new MigLayout("wrap " + crossword.getDimY(),                 // Layout Constraints
                "[grow][]0[grow]",   // Column constraints
                "[]0[shrink 1]");
        this.crossword = crossword;
        init();
    }

    private void init() {
        this.setLayout(migLayout);
        this.setPreferredSize(new Dimension(590, 400));
        this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
        setupDummyBoard();
    }

    //TODO Fix first row of tiles
    private void setupDummyBoard() {
        final Tile[][] theBoard = crossword.getSolution();
        tiles = new JTextField[crossword.getDimX()][crossword.getDimY()];
        JTextField tile = null;
        //Setting the board up
        for (int row = 0; row < crossword.getDimX(); row++) {
            for (int column = 0; column < crossword.getDimY(); column++) {
                if (theBoard[row][column].isBlocked()) {
                    tile = new JTextField("");
                    tile.setEnabled(false);
                    tile.setEditable(false);
                    tile.setBackground(Color.BLACK);
                } else {
                    tile = new JTextField();
                    tile.setText(Character.toString(theBoard[row][column].getLetter()));
                    switch (theBoard[row][column].getLetterType()) {
                        case START:
                            tile.addMouseListener(new MouseAdapter() {
                                @Override
                                public void mouseClicked(final MouseEvent mouseEvent) {
                                    logger.info("Start of word clicked. Need to unlock");
                                }
                            });
                            break;
                    }
                }
                //Setting preferred size does not work correctly
                //TODO Need to fix this
                tile.setPreferredSize(new Dimension(60, 60));
                tile.setSize(new Dimension(60, 60));
                tile.setHorizontalAlignment(JTextField.CENTER);
                tile.setFont(new Font("Calibri", Font.BOLD, 14));
                tile.setBorder(BorderFactory.createEtchedBorder());
                this.add(tile);
            }
        }
    }
}
